package com.hsy.leancloudchat.mvvmUtils

import android.app.Activity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding

abstract class HsyActivity<VB : ViewBinding, VM : HsyViewModel> : AppCompatActivity() {
    protected val mBinding by lazy {
        createViewBinding()!!
    }

    protected val mViewModel by lazy {
        createViewModel()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.v(TAG, "onCreate")
        setWindow()
        setContentView(mBinding.root)

    }

    override fun onStart() {
        super.onStart()
        Log.v(TAG, "onStart")

    }

    override fun onResume() {
        super.onResume()
        Log.v(TAG, "onResume")

    }

    override fun onPause() {
        super.onPause()
        Log.v(TAG, "onPause")

    }

    override fun onStop() {
        super.onStop()
        Log.v(TAG, "onStop")

    }

    override fun onDestroy() {
        super.onDestroy()
        Log.v(TAG, "onDestroy")

    }

    override fun onRestart() {
        super.onRestart()
        Log.v(TAG, "onRestart")

    }

    /**
     * 通过反射创建 viewBinding
     * @return  ViewBinding
     */
    open fun createViewBinding(): VB? {
        return createViewBinding(layoutInflater)
    }

    /**
     * 通过反射创建 viewModel
     * @return  ViewModel
     */
    open fun createViewModel(): VM {
        return createViewModel(this)
    }

    fun Activity.setWindow() {
        /** 设置全屏，设置状态栏常驻，设置状态栏字体颜色为深色 */
        window.setFlags(
            WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
            WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
        )
        window.decorView.systemUiVisibility =
            (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
        // 设置输入框能够被顶起
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
    }
}